package dubboRpc.provider;

import dubboRpc.common.CommonProtocal;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author: zlz
 * @date: 2021/9/15 22:20
 * @description:
 * @since: JDK1.8
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter{
    HelloServiceImpl helloService = new HelloServiceImpl();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String msgStr = msg.toString();
        System.out.println("msg is " + msgStr);
        if (msgStr.startsWith(CommonProtocal.PROTOCAL)) {
            String res = helloService.hello(msgStr.substring(msgStr.lastIndexOf("#") + 1));
            ctx.writeAndFlush(res);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
